package com.itheima.mall.web.pms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.pms.PmsProductAttributeService;
import com.itheima.mall.entity.domain.pms.ProductAttribute;
import com.itheima.mall.entity.page.MyPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("productAttribute")
public class PmsProductAttributeController {
    @DubboReference
    private PmsProductAttributeService productAttributeService;
    //根据分类查询属性列表或参数列表
    @GetMapping("list/{cid}")
    public Result getById(@PathVariable Integer cid, Integer pageNum, Integer pageSize, Integer type){
        MyPage<ProductAttribute> attributePage =  productAttributeService.getCategoryId(cid,pageNum,pageSize,type);
        return Result.success(attributePage);
    }
    //根据商品分类的id获取商品属性及属性分类
    @GetMapping("/attrInfo/{productCategoryId}")
    public Result getCategoryId(@PathVariable Integer productCategoryId ){
        List<ProductAttribute> productAttribute =   productAttributeService.getCategoryById(productCategoryId);
        return Result.success(productAttribute);
    }
    //添加商品属性信息
    @PostMapping("create")
    public Result create(@RequestBody ProductAttribute productAttribute){
        productAttributeService.save(productAttribute);
        return Result.success("添加成功");
    }
    //批量删除商品属性
    @PostMapping("delete")
    public Result delete(@RequestParam List<Integer> ids){
        productAttributeService.removeByIds(ids);
        return Result.success("删除成功");
    }
    //修改商品属性信息
    @PostMapping("/update/{id}")
    public Result update(@RequestBody Long id,@RequestBody ProductAttribute productAttribute){
        productAttribute.setId(id);
        productAttributeService.updateById(productAttribute);
        return Result.success("修改成功");
    }
    //查询单个商品属性
    @GetMapping("/{id}")
    public Result getById(Long id){
        ProductAttribute productAttribute = productAttributeService.getById(id);
        return Result.success(productAttribute);
    }
}
